using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Planar.PT120;

namespace Planar.PT120.Tray
{
    public partial class PT120Tray : Form
    {
        public PT120Tray()
        {
            InitializeComponent();

            Driver.Instance.CommandReceived += new EventHandler<CommandEventArgs>(Driver_CommandReceived);
            Driver.Instance.DriverError += new EventHandler<DriverErrorEventArgs>(Driver_DriverError);
        }

        void Driver_DriverError(object sender, DriverErrorEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.Exception, e.Message);
            ShowStatusMessage(e.Message, ToolTipIcon.Error);
        }

        void Driver_CommandReceived(object sender, CommandEventArgs e)
        {
            MouseSimulator.ProcessCommand(e.Command);
        }


        #region Menu Item Handlers
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (Driver.Instance.IsRunning)
            {
                startToolStripMenuItem.Enabled = false;
                stopToolStripMenuItem.Enabled = true;
                calibrateToolStripMenuItem.Enabled = true;
            }
            else
            {
                startToolStripMenuItem.Enabled = true;
                stopToolStripMenuItem.Enabled = false;
                calibrateToolStripMenuItem.Enabled = false;
            }

            portToolStripMenuItem.DropDownItems.Clear();
            foreach (string name in Driver.GetPortNames())
            {
                ToolStripMenuItem PortItem = new ToolStripMenuItem(name);
                PortItem.Click += new EventHandler(PortItem_Click);
                if (name == Driver.Instance.Name)
                {
                    PortItem.Enabled = false;
                }

                portToolStripMenuItem.DropDownItems.Add(PortItem);
            }
        }

        void PortItem_Click(object sender, EventArgs e)
        {
            Driver.Instance.Name = ((ToolStripMenuItem)sender).Text;
            ShowStatusMessage("Port changed to: " + Driver.Instance.Name, ToolTipIcon.Info);
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Driver.Instance.Start();
            ShowStatusMessage("Running...", ToolTipIcon.Info);
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Driver.Instance.Stop();
            ShowStatusMessage("Not Running...", ToolTipIcon.Info);
        } 
        
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Driver.Instance.Stop();
            Close();
        }
        private void calibrateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Calibrate c = new Calibrate();
            if (c.ShowDialog(this) == DialogResult.OK)
            {
                MouseSimulator.CalibrationX = c.CalibrationX;
                MouseSimulator.CalibrationY = c.CalibrationY;

                System.Diagnostics.Debug.WriteLine("CalibrationX: " + c.CalibrationX);
                System.Diagnostics.Debug.WriteLine("CalibrationY: " + c.CalibrationY);
            }
        }
        #endregion

        #region Tooltip messages
        private void ShowStatusMessage(string Message, ToolTipIcon Icon)
        {
            notifyIcon1.BalloonTipIcon = Icon;
            notifyIcon1.BalloonTipText = Message;
            notifyIcon1.ShowBalloonTip(3000);
        } 
        #endregion

        
    }
}